package cc.rengu.igas.channel.nucc.core.realize.impl;


import cc.rengu.igas.channel.nucc.common.constant.*;
import cc.rengu.igas.channel.nucc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.facade.bean.*;
import cc.rengu.igas.channel.nucc.facade.request.*;
import cc.rengu.igas.channel.nucc.facade.response.*;
import cc.rengu.jradp.schedule.Schedule;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.CertInfoMapper;
import cc.rengu.oltp.service.common.dao.SecPlanInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.CertInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.SecPlanInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.CertInfo;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.SecPlanInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.AESUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.Optional;

public class NuccServiceImpl implements NuccService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String origTxnType;

    public NuccServiceImpl() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        origTxnType = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TXN_NUM);
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        /* 获取通道配置信息 */
//        DstChannelInfoMapper dstChannelInfoMapper = new DstChannelInfoMapperImpl();
        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(NuccTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);
        if (null == dstChannelCfg) {
            rglog.error("获取通道<{}>配置信息失败!", NuccParamConstant.TUNL_ID_NUCC);
            throw new BizException(RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespCode(), RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespDesc());
        }
        /* 根据通道安全计划获取安全计划 */
        SecPlanInfoMapper secPlanInfoMapper = new SecPlanInfoMapperImpl();
        List<SecPlanInfo> secPlanInfoList = secPlanInfoMapper.selectSecPlanInfoBySecPlanId(instId, dstChannelCfg.getSecPlanId());
        String signType = XmlConfigUtil.getXmlConfigCacheKeyValue(NuccConfigConstant.NUCC_CONFIG, NuccConfigConstant.SIGN_TYPE);
        if (null != secPlanInfoList && !secPlanInfoList.isEmpty()) {
            xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.SEC_PLAN_INFO_LIST, JSON.toJSONString(secPlanInfoList));
            /* 加签证书 */
            Optional<SecPlanInfo> secPlanInfoOpt = secPlanInfoList.stream().filter(item -> item.getSecPlanType().equals(NuccParamConstant.SIGN_CERT_TYPE)).findFirst();
            if (secPlanInfoOpt.isPresent()) {
                CertInfoMapper certInfoMapper = new CertInfoMapperImpl();
                CertInfo certInfo = certInfoMapper.selectCertInfoByPrimaryKey(secPlanInfoOpt.get().getSecPlanId() + secPlanInfoOpt.get().getSecPlanType());
                if (null != certInfo) {
                    //设置签名的证书序列号
                    xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_SIGN_SN, certInfo.getCertDn());
                    xmlTreeUtil.setXmlTreeObjectValue(NuccTreeNodeConstant.EPCC_PRIVATE_CER_INFO, certInfo);
                }
            }

            /* 敏感数据加密证书 */
            secPlanInfoOpt = secPlanInfoList.stream().filter(item -> item.getSecPlanType().equals(NuccParamConstant.ENCRYPT_CERT_TYPE)).findFirst();
            if (secPlanInfoOpt.isPresent()) {
                CertInfoMapper certInfoMapper = new CertInfoMapperImpl();
                CertInfo certInfo = certInfoMapper.selectCertInfoByPrimaryKey(secPlanInfoOpt.get().getSecPlanId() + secPlanInfoOpt.get().getSecPlanType());
                if (null != certInfo) {
                    //设置加密证书序列号
                    xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_SN, certInfo.getCertDn());
                    xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY, certInfo.getCertValue());
                }
            }
        }
    }

    @Override
    public TransSeqNoApplyResponse transSeqNoApply(TransSeqNoApplyRequest transSeqNoApplyRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ConvertUtil.convertInput(transSeqNoApplyRequest);
        /* 设置银联全渠道的交易码，并调用银联全渠道服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "40100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_40100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        TransSeqNoApplyResponse transSeqNoApplyResponse = new TransSeqNoApplyResponse();
        ConvertUtil.convertOutput(transSeqNoApplyResponse);
        return transSeqNoApplyResponse;
    }

    @Override
    public VerifySignAcqResponse verifySignQuick(VerifySignAcqRequest verifySignAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (verifySignAcqRequest.getSgnInf() != null) {
            verifySignAcqRequest.setSgnInf(encryptSgnInf(verifySignAcqRequest.getSgnInf()));
        }
        if (verifySignAcqRequest.getInstgInf() != null) {
            verifySignAcqRequest.setInstgInf(encryptInstgInf(verifySignAcqRequest.getInstgInf()));
        }
        ConvertUtil.convertInput(verifySignAcqRequest);
/*        NuccSeqNoManage epccSeqNoManage = new NuccSeqNoManageImpl();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.CHANNEL_SEQ_NUM,epccSeqNoManage.genEpccSeqNo());
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_TRX_ID,epccSeqNoManage.genEpccSeqNo());*/
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "10100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_10100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        VerifySignAcqResponse verifySignAcqResponse = new VerifySignAcqResponse();
        ConvertUtil.convertOutput(verifySignAcqResponse);
        return verifySignAcqResponse;
    }

    @Override
    public VerifySignGwResponse verifySignGw(VerifySignAcqRequest verifySignAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (verifySignAcqRequest.getSgnInf() != null) {
            verifySignAcqRequest.setSgnInf(encryptSgnInf(verifySignAcqRequest.getSgnInf()));
        }
        if (verifySignAcqRequest.getInstgInf() != null) {
            verifySignAcqRequest.setInstgInf(encryptInstgInf(verifySignAcqRequest.getInstgInf()));
        }
        ConvertUtil.convertInput(verifySignAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "10100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_10100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        VerifySignGwResponse verifySignGwResponse = new VerifySignGwResponse();
        ConvertUtil.convertOutput(verifySignGwResponse);
        return verifySignGwResponse;
    }

    @Override
    public CommonResponse businessOffAuthSign(BusinessAuthSignAcqRequest businessAuthSignAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        encryptObject(businessAuthSignAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "11100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_11100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        CommonResponse commonResponse = new CommonResponse();
        ConvertUtil.convertOutput(commonResponse);
        return commonResponse;
    }

    @Override
    public VerifySignGwResponse businessGwAuthSign(BusinessAuthSignAcqRequest businessAuthSignAcqRequest) throws Exception {
        return null;
    }

    @Override
    public BusiAuthSignQuickAcqResponse businessAuthSignQuick(BusinessAuthSignAcqRequest businessAuthSignAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        encryptObject(businessAuthSignAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "11100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_11100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        BusiAuthSignQuickAcqResponse busiAuthSignQuickAcqResponse = new BusiAuthSignQuickAcqResponse();
        ConvertUtil.convertOutput(busiAuthSignQuickAcqResponse);
        return busiAuthSignQuickAcqResponse;
    }

    @Override
    public RescindSignAcqResponse rescindSign(RescindSignAcqRequest rescindSignRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (rescindSignRequest.getInstgInf() != null) {
            rescindSignRequest.setInstgInf(encryptInstgInf(rescindSignRequest.getInstgInf()));
        }
        ConvertUtil.convertInput(rescindSignRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "10300101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_10300101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        RescindSignAcqResponse rescindSignAcqResponse = new RescindSignAcqResponse();
        ConvertUtil.convertOutput(rescindSignAcqResponse);
        return rescindSignAcqResponse;
    }

    @Override
    public SignStateQueryAcqResponse signStateQuery(SignStateQueryAcqRequest signStateQueryAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ConvertUtil.convertInput(signStateQueryAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "33100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_33100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        SignStateQueryAcqResponse signStateQueryAcqResponse = new SignStateQueryAcqResponse();
        ConvertUtil.convertOutput(signStateQueryAcqResponse);
        if (signStateQueryAcqResponse.getSignInfo() != null) {
            /*解密签约信息*/
            signStateQueryAcqResponse.setSignInfo(descryptSgnInf(signStateQueryAcqResponse.getSignInfo()));
        }
        return signStateQueryAcqResponse;
    }

    @Override
    public CommonTransAcqResponse agreementPay(AgreementPayAcqRequest agreementPayAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ConvertUtil.convertInput(agreementPayAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "20100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_20100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        CommonTransAcqResponse commonTransAcqResponse = new CommonTransAcqResponse();
        ConvertUtil.convertOutput(commonTransAcqResponse);
        return commonTransAcqResponse;
    }

    @Override
    public CommonTransAcqResponse gwPayApply(GwPayApplyRequest gwPayApplyRequest) throws Exception {
        return null;
    }

    @Override
    public CommonTransAcqResponse mchntPayment(MchntPaymentAcqRequest mchntPaymentAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (mchntPaymentAcqRequest.getPyerInf() != null) {
            mchntPaymentAcqRequest.setPyerInf(encryptPyerInf(mchntPaymentAcqRequest.getPyerInf()));
        }
        if (mchntPaymentAcqRequest.getPyeeAcctInf() != null) {
            mchntPaymentAcqRequest.setPyeeAcctInf(encryptPyeeAcctInf(mchntPaymentAcqRequest.getPyeeAcctInf()));
        }
        ConvertUtil.convertInput(mchntPaymentAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "21100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_21100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        CommonTransAcqResponse commonTransAcqResponse = new CommonTransAcqResponse();
        ConvertUtil.convertOutput(commonTransAcqResponse);
        return commonTransAcqResponse;
    }

    @Override
    public CommonTransAcqResponse refundPayment(RefundPaymentAcqRequest refundPaymentAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (refundPaymentAcqRequest.getPyerInf() != null) {
            refundPaymentAcqRequest.setPyerInf(encryptPyerInf(refundPaymentAcqRequest.getPyerInf()));
        }

        ConvertUtil.convertInput(refundPaymentAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "20500101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_20500101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        CommonTransAcqResponse commonTransAcqResponse = new CommonTransAcqResponse();
        ConvertUtil.convertOutput(commonTransAcqResponse);
        return commonTransAcqResponse;
    }

    @Override
    public TransStateQueryAcqResponse transStateQuery(TransStateQueryAcqRequest transStateQueryAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ConvertUtil.convertInput(transStateQueryAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "30100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_30100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        TransStateQueryAcqResponse transStateQueryAcqResponse = new TransStateQueryAcqResponse();
        ConvertUtil.convertOutput(transStateQueryAcqResponse);
        if (transStateQueryAcqResponse.getPyerAcctId() != null) {
            /*解密信息*/
            transStateQueryAcqResponse.setPyerAcctId(descryptStr(transStateQueryAcqResponse.getPyerAcctId()));
        }
        if (transStateQueryAcqResponse.getPyerAcctNm() != null) {
            /*解密信息*/
            transStateQueryAcqResponse.setPyerAcctNm(descryptStr(transStateQueryAcqResponse.getPyerAcctNm()));
        }
        return transStateQueryAcqResponse;
    }

    @Override
    public AbnormalStateQueryAcqResponse abnormalStateQuery(AbnormalStateQueryAcqRequest abnormalStateQueryAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ConvertUtil.convertInput(abnormalStateQueryAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "31100101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_31100101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        AbnormalStateQueryAcqResponse abnormalStateQueryAcqResponse = new AbnormalStateQueryAcqResponse();
        ConvertUtil.convertOutput(abnormalStateQueryAcqResponse);
        return abnormalStateQueryAcqResponse;
    }

    @Override
    public TransDetailQueryAcqResponse transDetailQuery(TransDetailQueryAcqRequest transDetailQueryAcqRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ConvertUtil.convertInput(transDetailQueryAcqRequest);
        /* 设置网联的交易码，并调用网联客户端服务 */
        Schedule schedule = new Schedule();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "31300101");
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_MSG_TP, NuccTxnNumConstant.MSG_31300101);
        int iReturnCode = schedule.callService(NuccServiceConstant.EPCC_CLIENT_SERVICE, 0);
        /* 恢复原交易码 */
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, origTxnType);
        if (0 != iReturnCode) {
            rglog.error("调用网联清算平台服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        TransDetailQueryAcqResponse response = new TransDetailQueryAcqResponse();
        ConvertUtil.convertOutput(response);
        if (response.getInstgAcctId() != null) {
            /*解密信息*/
            response.setInstgAcctId(descryptStr(response.getInstgAcctId()));
        }
        if (response.getInstgAcctNm() != null) {
            /*解密信息*/
            response.setInstgAcctNm(descryptStr(response.getInstgAcctNm()));
        }
        return response;
    }

    private void encryptObject(BusinessAuthSignAcqRequest businessAuthSignAcqRequest) throws Exception {
        String aesKey = null;
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (xmlTreeUtil.isExist(NuccTreeNodeConstant.EPCC_ENC_KEY)) {
            aesKey = xmlTreeUtil.getXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY);
        } else {
            //随机生成AES对称密钥
            aesKey = AESUtil.generateAesKey(256);
            xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY, aesKey);
        }
        if (businessAuthSignAcqRequest.getSgnInf() != null) {
            businessAuthSignAcqRequest.setSgnInf(encryptSgnInf(businessAuthSignAcqRequest.getSgnInf()));
        }
        if (businessAuthSignAcqRequest.getInstgInf() != null) {
            businessAuthSignAcqRequest.setInstgInf(encryptInstgInf(businessAuthSignAcqRequest.getInstgInf()));
        }
        if (businessAuthSignAcqRequest.getTrxInf() != null) {
            businessAuthSignAcqRequest.setTrxInf(encryptTrxInf(businessAuthSignAcqRequest.getTrxInf()));
        }
//        if(businessAuthSignAcqRequest.getIdNo() != null){
//            businessAuthSignAcqRequest.setIdNo(AESUtil.pkcs5EcbEncryptData(aesKey,businessAuthSignAcqRequest.getIdNo()));
//        }
        ConvertUtil.convertInput(businessAuthSignAcqRequest);
    }

    /**
     * 解密信息
     *
     * @param encStr 待解密字串
     * @return 解密后的字串
     * @throws Exception
     */
    private String descryptStr(String encStr) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        //获取网联的对称密钥
        String aesKey = xmlTreeUtil.getXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY);
        if (aesKey != null) {
            if (aesKey.startsWith("01")) {
                //AES加密
                return AESUtil.pkcs5EcbDecryptData(aesKey.substring(3), encStr);
            } else {
                rglog.error("暂时未实现国密算法");
                return null;
            }

        }
        return encStr;
    }

    /**
     * 加密签约信息
     *
     * @param signInfo
     * @return
     * @throws Exception
     */
    private SignInfoBean encryptSgnInf(SignInfoBean signInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String aesKey = null;
        if (signInfo.getSgnAcctNm() != null ||
                signInfo.getMobNo() != null ||
                signInfo.getIdNo() != null ||
                signInfo.getLglRepNm() != null ||
                signInfo.getSgnAcctId() != null ||
                signInfo.getLglRepIDNo() != null) {
            if (xmlTreeUtil.isExist(NuccTreeNodeConstant.EPCC_ENC_KEY)) {
                aesKey = xmlTreeUtil.getXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY);
            } else {
                //随机生成AES对称密钥
                aesKey = AESUtil.generateAesKey(256);
                xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY, aesKey);
            }

            if (signInfo.getSgnAcctNm() != null) {
                signInfo.setSgnAcctNm(AESUtil.pkcs5EcbEncryptData(aesKey, signInfo.getSgnAcctNm()));
            }
            if (signInfo.getMobNo() != null) {
                signInfo.setMobNo(AESUtil.pkcs5EcbEncryptData(aesKey, signInfo.getMobNo()));
            }
            if (signInfo.getIdNo() != null) {
                signInfo.setIdNo(AESUtil.pkcs5EcbEncryptData(aesKey, signInfo.getIdNo()));
            }
            if (signInfo.getLglRepNm() != null) {
                signInfo.setLglRepNm(AESUtil.pkcs5EcbEncryptData(aesKey, signInfo.getLglRepNm()));
            }
            if (signInfo.getSgnAcctId() != null) {
                signInfo.setSgnAcctId(AESUtil.pkcs5EcbEncryptData(aesKey, signInfo.getSgnAcctId()));
            }
            if (signInfo.getLglRepIDNo() != null) {
                signInfo.setLglRepIDNo(AESUtil.pkcs5EcbEncryptData(aesKey, signInfo.getLglRepIDNo()));
            }
        }

        return signInfo;
    }

    /**
     * 解密签约信息
     *
     * @param signInfo
     * @return
     * @throws Exception
     */
    private SignInfoBean descryptSgnInf(SignInfoBean signInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        //获取网联的对称密钥
        String aesKey = xmlTreeUtil.getXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY);
        if (aesKey != null) {
            if (aesKey.startsWith("01")) {
                //AES加密
                if (signInfo.getSgnAcctNm() != null) {
                    signInfo.setSgnAcctNm(AESUtil.pkcs5EcbDecryptData(aesKey.substring(3), signInfo.getSgnAcctNm()));
                }
                if (signInfo.getMobNo() != null) {
                    signInfo.setMobNo(AESUtil.pkcs5EcbDecryptData(aesKey.substring(3), signInfo.getMobNo()));
                }
                if (signInfo.getIdNo() != null) {
                    signInfo.setIdNo(AESUtil.pkcs5EcbDecryptData(aesKey.substring(3), signInfo.getIdNo()));
                }
                if (signInfo.getLglRepNm() != null) {
                    signInfo.setLglRepNm(AESUtil.pkcs5EcbDecryptData(aesKey.substring(3), signInfo.getLglRepNm()));
                }
                if (signInfo.getSgnAcctId() != null) {
                    signInfo.setSgnAcctId(AESUtil.pkcs5EcbDecryptData(aesKey.substring(3), signInfo.getSgnAcctId()));
                }
                if (signInfo.getLglRepIDNo() != null) {
                    signInfo.setLglRepIDNo(AESUtil.pkcs5EcbDecryptData(aesKey.substring(3), signInfo.getLglRepIDNo()));
                }


            } else {
                rglog.error("暂时未实现国密算法");
                return null;
            }

        }
        return signInfo;
    }


    /**
     * 加密账户信息
     *
     * @param instgInf
     * @return
     * @throws Exception
     */
    private InstgInfBean encryptInstgInf(InstgInfBean instgInf) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String aesKey = null;
        if (instgInf.getInstgAcct() != null) {
            if (xmlTreeUtil.isExist(NuccTreeNodeConstant.EPCC_ENC_KEY)) {
                aesKey = xmlTreeUtil.getXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY);
            } else {
                //随机生成AES对称密钥
                aesKey = AESUtil.generateAesKey(256);
                xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY, aesKey);
            }
            instgInf.setInstgAcct(AESUtil.pkcs5EcbEncryptData(aesKey, instgInf.getInstgAcct()));
        }
        return instgInf;
    }

    /**
     * 加密付款方信息
     *
     * @param pyerInf
     * @return
     * @throws Exception
     */
    private PyerInfBean encryptPyerInf(PyerInfBean pyerInf) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String aesKey = null;
        if (pyerInf.getPyerMobNo() != null ||
                pyerInf.getPyerAcctNm() != null ||
                pyerInf.getPyerIDNo() != null ||
                pyerInf.getPyerAcctId() != null ||
                pyerInf.getPyerAcctNo() != null ||
                pyerInf.getPyerNm() != null) {
            if (xmlTreeUtil.isExist(NuccTreeNodeConstant.EPCC_ENC_KEY)) {
                aesKey = xmlTreeUtil.getXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY);
            } else {
                //随机生成AES对称密钥
                aesKey = AESUtil.generateAesKey(256);
                xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY, aesKey);
            }

            if (pyerInf.getPyerMobNo() != null) {
                pyerInf.setPyerMobNo(AESUtil.pkcs5EcbEncryptData(aesKey, pyerInf.getPyerMobNo()));
            }
            if (pyerInf.getPyerAcctNm() != null) {
                pyerInf.setPyerAcctNm(AESUtil.pkcs5EcbEncryptData(aesKey, pyerInf.getPyerAcctNm()));
            }
            if (pyerInf.getPyerIDNo() != null) {
                pyerInf.setPyerIDNo(AESUtil.pkcs5EcbEncryptData(aesKey, pyerInf.getPyerIDNo()));
            }
            if (pyerInf.getPyerAcctId() != null) {
                pyerInf.setPyerAcctId(AESUtil.pkcs5EcbEncryptData(aesKey, pyerInf.getPyerAcctId()));
            }
            if (pyerInf.getPyerAcctNo() != null) {
                pyerInf.setPyerAcctNo(AESUtil.pkcs5EcbEncryptData(aesKey, pyerInf.getPyerAcctNo()));
            }
            if (pyerInf.getPyerNm() != null) {
                pyerInf.setPyerNm(AESUtil.pkcs5EcbEncryptData(aesKey, pyerInf.getPyerNm()));
            }
        }

        return pyerInf;
    }

    /**
     * 加密交易信息
     *
     * @param trxInfoBean
     * @return
     * @throws Exception
     */
    private TrxInfoBean encryptTrxInf(TrxInfoBean trxInfoBean) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String aesKey = null;
        if (trxInfoBean.getPyeeIdNo() != null) {
            if (xmlTreeUtil.isExist(NuccTreeNodeConstant.EPCC_ENC_KEY)) {
                aesKey = xmlTreeUtil.getXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY);
            } else {
                //随机生成AES对称密钥
                aesKey = AESUtil.generateAesKey(256);
                xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY, aesKey);
            }

            if (trxInfoBean.getPyeeIdNo() != null) {
                trxInfoBean.setPyeeIdNo(AESUtil.pkcs5EcbEncryptData(aesKey, trxInfoBean.getPyeeIdNo()));
            }

        }

        return trxInfoBean;
    }


    /**
     * 加密收款方信息
     *
     * @param pyeeAcctInf
     * @return
     * @throws Exception
     */
    private PyeeAcctInfBean encryptPyeeAcctInf(PyeeAcctInfBean pyeeAcctInf) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String aesKey = null;
        if (pyeeAcctInf.getPyeeBkNm() != null || pyeeAcctInf.getPyeeBkNo() != null) {
            if (xmlTreeUtil.isExist(NuccTreeNodeConstant.EPCC_ENC_KEY)) {
                aesKey = xmlTreeUtil.getXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY);
            } else {
                //随机生成AES对称密钥
                aesKey = AESUtil.generateAesKey(256);
                xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ENC_KEY, aesKey);
            }

            if (pyeeAcctInf.getPyeeBkNm() != null) {
                pyeeAcctInf.setPyeeBkNm(AESUtil.pkcs5EcbEncryptData(aesKey, pyeeAcctInf.getPyeeBkNm()));
            }
            if (pyeeAcctInf.getPyeeBkNo() != null) {
                pyeeAcctInf.setPyeeBkNo(AESUtil.pkcs5EcbEncryptData(aesKey, pyeeAcctInf.getPyeeBkNo()));
            }

        }
        return pyeeAcctInf;
    }
}
